package com.jijuxie.house.mapper;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jijuxie.house.domain.HouseInfo;
import com.jijuxie.house.domain.dto.HouseSearchDTO;
import com.jijuxie.house.domain.vo.HouseListVO;

@Mapper
public interface HouseMapper extends BaseMapper<HouseInfo> {

    /**
     * 查询房源列表
     */
    List<HouseInfo> selectHouseList(HouseInfo houseInfo);

    /**
     * 查询房源详情
     */
    HouseInfo selectHouseById(@Param("houseId") Long houseId);

    /**
     * 搜索房源
     */
    List<HouseInfo> searchHouseList(HouseSearchDTO searchDTO);

    /**
     * 查询收藏列表
     */
    List<HouseInfo> selectCollectList(@Param("userId") Long userId);

    /**
     * 新增房源
     */
    @Insert("insert into house_info ("
            + "agent_id, title, house_type, property_type, "
            + "province, city, district, address, "
            + "area, price, price_unit, bedroom, living_room, bathroom, "
            + "floor, total_floor, orientation, decoration, "
            + "description, status, "
            + "create_by, update_by, create_time, update_time, "
            + "del_flag, lng, lat) "
            + "values ("
            + "#{agentId}, #{title}, #{houseType}, #{propertyType}, "
            + "#{province}, #{city}, #{district}, #{address}, "
            + "#{area}, #{price}, #{priceUnit}, #{bedroom}, #{livingRoom}, #{bathroom}, "
            + "#{floor}, #{totalFloor}, #{orientation}, #{decoration}, "
            + "#{description}, #{status}, "
            + "#{createBy}, #{updateBy}, sysdate(), sysdate(), "
            + "'0', #{lng}, #{lat})")
    int insertHouse(HouseInfo houseInfo);

    /**
     * 查询已删除的房源列表
     */
    List<HouseInfo> selectDeletedList(HouseInfo houseInfo);

    /**
     * 查询已删除的房源
     */
    @Select("SELECT * FROM house_info WHERE del_flag = '1' ORDER BY update_time DESC")
    Page<HouseInfo> selectDeletedPage(Page<HouseInfo> page);

    @Update("UPDATE house_info SET del_flag = '0', update_time = #{updateTime}, update_by = #{updateBy} WHERE house_id = #{houseId} AND del_flag = '1'")
    int restoreHouse(@Param("houseId") Long houseId,
            @Param("updateTime") LocalDateTime updateTime,
            @Param("updateBy") String updateBy);

    /**
     * 查询房源列表（包含详细信息）
     *
     * @param userId 当前用户ID
     * @param isHot 是否查询热门房源
     * @return 房源列表
     */
    List<HouseListVO> selectHouseListWithDetail(@Param("userId") Long userId, @Param("isHot") Boolean isHot);

    /**
     * 增加房源收藏数
     */
    @Update("UPDATE house_info SET collect_count = collect_count + 1 WHERE house_id = #{houseId}")
    int addCollectCount(@Param("houseId") Long houseId);

    /**
     * 减少房源收藏数
     */
    @Update("UPDATE house_info SET collect_count = GREATEST(0, collect_count - 1) WHERE house_id = #{houseId}")
    int reduceCollectCount(@Param("houseId") Long houseId);

    /**
     * 批量查询房源封面图片
     *
     * @param houseIds 房源ID列表
     * @return 封面图片列表，包含house_id和image_url
     */
    List<Map<String, Object>> selectHouseCoverImages(@Param("houseIds") List<Long> houseIds);
}
